package com.bix.util.blizfiles.dbc;

/**
 * This is an abstract class that represents a record in a DBC file.
 * 
 * @author squid
 * 
 * @version 1.0.0
 */
public abstract class DBCRecord {
	protected DBCRecordField[] fields;

	public DBCRecord() {
	}

	/**
	 * Reads the fields from the dbc file.
	 * 
	 * @param dbc
	 *          The DBC file to read the record from.
	 */
	protected void read (DBCFile dbc) {
		DBCRecordField[] fields = this.getFields ();
		for (DBCRecordField field : fields) {
			field.read (dbc);
		}
	}

	/**
	 * Return the array of fields from a DBC file record.
	 * 
	 * @return The array of fields.
	 */
	protected DBCRecordField[] getFields () {
		return this.fields;
	}

	/**
	 * Returns the value of a particular field.
	 * 
	 * @param fieldName
	 *          The name of the field.
	 * 
	 * @return The value of the field.
	 */
	protected Object getFieldValue (String fieldName) {
		for (DBCRecordField field : this.getFields ()) {
			if (field.getName ().equalsIgnoreCase (fieldName)) {
				return field.getValue ();
			}
		}
		return null;
	}

	public String toString () {
		String s = new String ();

		for (DBCRecordField field : this.getFields ()) {
			s += field.getName () + "[" + field.getValue () + "]";
		}

		return s;
	}
}
